package com.uniview.web.mobile.contorller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uniview.base.BaseController;
import com.uniview.entity.po.Employees;
import com.uniview.entity.po.ServiceOrders;
import com.uniview.entity.vo.OrderApprovalVO;
import com.uniview.entity.vo.OrderHandleVO;
import com.uniview.entity.vo.ResultMessage;
import com.uniview.service.EmployeesService;
import com.uniview.service.ServiceOrdersService;
import com.uniview.web.api.ServiceOrderApi;

@Controller
@RequestMapping(value = "/mobile/order")
public class ServicesOrderApiContorller extends BaseController {

	@Autowired
	private ServiceOrderApi serviceOrderApi;
	
	@Autowired
	private ServiceOrdersService srviceOrdersService;
	
	@Value("#{configProperties['uniview.deptLevel']}")
	private int level;

	@Autowired
	private EmployeesService employeesService;
	
	@InitBinder
	protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}

	/**
	 * 获取对外测试列表数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findExternalTestByworkNo")
	@ResponseBody
	public String findExternalTestByworkNo(HttpServletRequest request) {
		return serviceOrderApi.findExternalTestByworkNo(request);
	}

	/**
	 * 获取网上问题列表数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findOnlieProblemsByworkNo")
	@ResponseBody
	public String findOnlieProblemsByworkNo(HttpServletRequest request) {
		return serviceOrderApi.findOnlieProblemsByworkNo(request);
	}

	/**
	 * 获取项目交付列表数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findProjectDeliveryByworkNo")
	@ResponseBody
	public String findProjectDeliveryByworkNo(HttpServletRequest request) {
		return serviceOrderApi.findProjectDeliveryByworkNo(request);
	}

	/**
	 * 获取备件维修 列表数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findRmaByworkNo")
	@ResponseBody
	public String findRmaByworkNo(HttpServletRequest request) {
		return serviceOrderApi.findRmaByworkNo(request);
	}

	/**
	 * 查询所有的工单
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findServiceOrders")
	@ResponseBody
	public String findServiceOrders(HttpServletRequest request) {
		return serviceOrderApi.findServiceOrders(request);
	}

	/**
	 * 新增工单
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "/addOrder")
	@ResponseBody
	public ResultMessage addOrder(ServiceOrders order, @RequestParam(value="ccList[]") String[] ccList) {
		return serviceOrderApi.addOrder(order, ccList);
	}

	/**
	 * 删除工单
	 * 
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value = "/deleteOrder")
	@ResponseBody
	public ResultMessage deleteOrder(int orderId) {
		return serviceOrderApi.deleteOrder(orderId);
	}

	/**
	 * 修改工单
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "/updateOrder")
	@ResponseBody
	public ResultMessage updateOrder(ServiceOrders order) {
		return serviceOrderApi.updateOrder(order);
	}

	/**
	 * 审批工单
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/applyOrder")
	@ResponseBody
	public ResultMessage applyOrder(HttpServletRequest request) {
		return serviceOrderApi.applyOrder(request);
	}

	/**
	 * 催单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/urgeOrder")
	@ResponseBody
	public ResultMessage urgeOrder(HttpServletRequest request) {
		return serviceOrderApi.urgeOrder(request);
	}

	@RequestMapping(value = "/findOrderAndRole")
	@ResponseBody
	public ResultMessage findOrderAndEmployeeRole(Integer orderId, String workNo) {
		return serviceOrderApi.findOrderAndEmployeeRole(orderId, workNo);
	}

	@RequestMapping(value = "/findOrdersByWorkNo")
	@ResponseBody
	public ResultMessage findOrdersByWorkNo(HttpServletRequest request) {
		ResultMessage result = serviceOrderApi.findOrdersByWorkNo(request);
		// 处理部门名称
		List<ServiceOrders> list = (List<ServiceOrders>)result.getList();
		if (null != list && !list.isEmpty()) {
			for (int i=0; i<list.size(); i++) {
				ServiceOrders o = list.get(i);
				if (StringUtils.isNotBlank(o.getCreatorDept()) && o.getCreatorDept().contains("/")) {
					o.setCreatorDept(o.getCreatorDept().substring(o.getCreatorDept().lastIndexOf("/")));
				}
			}
		}
		return result;
	}

	@RequestMapping(value = "/findEmployeesBySearch")
	@ResponseBody
	public ResultMessage findEmployeesBySearch(String search, String mangerNo, Integer page, Integer limit) {
		return serviceOrderApi.findEmployeesBySearch(search, mangerNo, page, limit);
	}

	@RequestMapping(value = "/saveOrderApproval")
	@ResponseBody
	public ResultMessage saveOrderApproval(OrderApprovalVO voParam) {
		return serviceOrderApi.saveOrderApproval(voParam);
	}
	
	@RequestMapping(value = "/doOrderSign")
	@ResponseBody
	public ResultMessage doOrderSign(String workNo, Integer orderId, String address, Integer isWarning) {
		return this.serviceOrderApi.doOrderSign(workNo, orderId, address, isWarning);
	}

	@RequestMapping(value = "/findOfficeByQuery")
	@ResponseBody
	public ResultMessage findOfficeByQuery(String queryString,int page,int limit) {
		return serviceOrderApi.findOfficeByQuery(queryString, page, limit);
	}
	
	@RequestMapping(value = "/checkIsManager")
	@ResponseBody
	public ResultMessage checkIsManager(HttpServletRequest request){
		return serviceOrderApi.checkIsManager(request);
	}
	
	@RequestMapping(value = "/findEmployeesByWorkNo")
	@ResponseBody
	public ResultMessage findEmployees(String workNo){
		return serviceOrderApi.findEmployees(workNo);
	}
	
	@RequestMapping(value = "/saveOrderHandle")
	@ResponseBody
	public ResultMessage saveOrderHandle(OrderHandleVO vo, @RequestParam(value="medias[]") String[] medias, int isWarnning, String address) {
		return serviceOrderApi.saveOrderHandle(vo, medias, isWarnning, address);
	}
	
	@RequestMapping(value = "/findOrderById")
	@ResponseBody
	public ResultMessage findOrderById(int orderId){
		return serviceOrderApi.findOrderById(orderId);
	}
	
	@RequestMapping(value = "/findOrderDetailAndLogsByOrderId")
	@ResponseBody
	public ResultMessage findOrderDetailAndLogsByOrderId(int orderId){
		ResultMessage result = serviceOrderApi.findOrderDetailAndLogsByOrderId(orderId);
		ServiceOrders order = (ServiceOrders)result.getObj();
		if (null != order) {
			if (order.getCreatorDept().contains("/")) {
				order.setCreatorDept(order.getCreatorDept().substring(order.getCreatorDept().lastIndexOf("/")));
			}
		}
		return result;
	}
	
	@RequestMapping(value = "/countOrdersByStatusFromDay")
	@ResponseBody
	public Map<String,Object> countOrdersByStatusFromDay(HttpServletRequest request){
		try{
			int days = ServletRequestUtils.getIntParameter(request, "days", 7);
			String empCode = ServletRequestUtils.getStringParameter(request, "empCode");
			Employees emp = employeesService.findEmployeesByName(empCode.toUpperCase());
			String region = emp.feachOffice(level);
			Map<String, Object> ordersCount = srviceOrdersService.countOrdersByStatusFromDay(empCode.toUpperCase(),region,days);
			ordersCount=serviceOrderApi.fillStatus(ordersCount);
			return ordersCount;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value = "/statisticsOrderCount")
	@ResponseBody
	public Map<String,String> statisticsOrderCount(HttpServletRequest request){
		try{
			String empCode = ServletRequestUtils.getStringParameter(request, "empCode");
			return srviceOrdersService.statisticsOrderCount(empCode);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value = "/cc/get")
	@ResponseBody
	public ResultMessage getCCByUsername(String username) {
		return this.serviceOrderApi.getCCByUser(username);
	}
}
